package code1.binary_tree;

/**
 * 原题链接：https://leetcode.cn/problems/path-sum/
 */
public class P112 {

    public boolean isOk(TreeNode root,int curSum,int targetSum){        //curSum为当前的和
        if(root==null){
            return false;
        }
        if(root.left==null && root.right==null && curSum+root.val==targetSum){      //是叶子节点且路径和为目标数
            return true;
        }
        //继续往左子树搜，往右子树搜
        return isOk(root.left,root.val+curSum,targetSum) || isOk(root.right,root.val+curSum,targetSum);
    }

    public boolean hasPathSum(TreeNode root, int targetSum) {
        return isOk(root,0,targetSum);
    }
}
